package com.wipz.eoa.iphone.news;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.toft.core2.dao.SqlUtils;
import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.IBaseService;
import com.wipz.eoa.iphone.util.Constants;
import com.wipz.eoa.iphone.util.DateUtil;
import com.wipz.eoa.iphone.util.UUIDHexGenerator;

/**
 * 
 * @author Biqing Zeng
 * @date 2012-1-13 ����05:47:50
 */
public class NewsViewAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	private IBaseService baseService;
	
	private INewsViewService newsViewService;
	
	private Map bxSubmit;
	
	private Map insert;


	public Map getInsert() {
		return insert;
	}

	public void setInsert(Map insert) {
		this.insert = insert;
	}

	public Map getBxSubmit() {
		return bxSubmit;
	}

	public void setBxSubmit(Map bxSubmit) {
		this.bxSubmit = bxSubmit;
	}

	/**
	 * ֪ͨ�����б�
	 */
	private List list;
	/**
	 * ֪ͨ������Ϣ����
	 */
	private Object detail;
	
	/**
	 * ���������б�
	 */
	private List publish;
	
	private List manage;

	public List getManage() {
		return manage;
	}

	public void setManage(List manage) {
		this.manage = manage;
	}

	public List getPublish() {
		return publish;
	}

	public void setPublish(List publish) {
		this.publish = publish;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public Object getDetail() {
		return detail;
	}

	public void setDetail(Object detail) {
		this.detail = detail;
	}

	public void setNewsViewService(INewsViewService newsViewService) {
		this.newsViewService = newsViewService;
	}
	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}


	/**
	 * ���������б�
	 * @return
	 * 2012-2-1 ����06:54:08
	 */
	public String publish() {
		String qcontent = this.getRequest().getParameter("qcontent");
		try {
			String s = new String(qcontent.getBytes("iso-8859-1"),"utf-8");
			qcontent= java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		Map<String,String>	parms = new HashMap<String,String>();
		parms.put("COMMITERCODE", COMMITERCODE);
		parms.put("qcontent", qcontent);
		publish = baseService.queryList("news.list", parms);
		return SUCCESS;
	}
	
	public String insert() {
		Map<String,Object> params = new HashMap<String,Object>();
		String id = UUIDHexGenerator.getInstance().generate();
		String billCode = Constants.getBillCode(Constants.NEWS);
		String agencyId = this.getRequest().getParameter("AGENCYID");
		String  COMMITERCODE = (String) this.getSession().get(Constants.USERINFO_USERCODE);
		String urgenttype = this.getRequest().getParameter("URGENTTYPE");
		String contents = this.getRequest().getParameter("CONTENTS");
		String title = this.getRequest().getParameter("TITLE");
		
		params.put("ID", id);
		params.put("BILLCODE", billCode); //生成单据CODE
		params.put("PROCESS_STATUS", "0");
		params.put("CREATEDATE", DateUtil.currDateTime());
		params.put("TITLE", title);
		params.put("COMMITER", this.getRequest().getParameter("COMMITER"));
		params.put("COMMITERCODE",COMMITERCODE);
		params.put("DEPARTMENT", this.getRequest().getParameter("DEPARTMENT"));
		params.put("OWNERDEPT", agencyId);
		params.put("NEWSTYPE", this.getRequest().getParameter("NEWSTYPE"));
		params.put("URGENTTYPE", urgenttype);
		params.put("CONTENTS", contents);
		
		baseService.insert("news.insert", params);
		
		// 摘要内容
		StringBuffer c = new StringBuffer("");
		// 紧急程度
		String urgentType = "0".equals(urgenttype)? "普通":"紧急";
		if(!urgentType.isEmpty())
		{
			c.append("[").append(urgentType).append("]");
		}
		// 公告标题
		if(!title.isEmpty())
		{
			c.append(title);
		}
		
		String nextoperator = newsViewService.submit(Constants.PUBLICNEWS_BUSINESS_ID, id, billCode, COMMITERCODE, agencyId, c.toString());
		
		Map<String,String> m = new HashMap<String,String>();
		m.put("ID", id);
		m.put("PROCESS_STATUS", "1");
		baseService.update("news.changeprocessstatus", m);
		
		insert = new HashMap();
		insert.put("success", true);
		insert.put("NEXTOPERATOR", nextoperator);
		return SUCCESS;
	}
	
	public String manage() {
		String qcontent = this.getRequest().getParameter("qcontent");
		try {
			String s = new String(qcontent.getBytes("iso-8859-1"),"utf-8");
			qcontent= java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		Map<String,String>	parms = new HashMap<String,String>();
		parms.put("COMMITERCODE", COMMITERCODE);
		parms.put("qcontent", qcontent);
		manage = baseService.queryList("news.listmanage", parms);
		return SUCCESS;
	}
	
	
	/**
	 * ȡ��֪ͨ�����б���
	 * @return
	 * 2012-1-13 ����05:09:55
	 */
	public String list() {
		String qTitle = this.getRequest().getParameter("qTitle");
		try {
			String s = new String(qTitle.getBytes("iso-8859-1"),"utf-8");
			qTitle = java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		Map<String,String>	parms = new HashMap<String,String>();
		parms.put("COMMITERCODE", COMMITERCODE);
		parms.put("qTitle", qTitle);
		list = baseService.queryList("news.query", parms);
		return SUCCESS;
	}
	
	/**
	 * 更新通知公告状态
	 * @return
	 * 2012-2-19 下午04:08:24
	 */
	public String detail() {
		String id = this.getRequest().getParameter("id");
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(id == null || id.isEmpty() || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}else {
			Map<String,String>	parms = new HashMap<String,String>();
			parms.put("id", id);
			Map<String,String> map = new HashMap<String,String>();
			String newId = UUIDHexGenerator.getInstance().generate();
			map.put("ID", newId);
			map.put("NEWSID", id);
			map.put("COMMITERCODE", COMMITERCODE);
			Integer num = (Integer) baseService.queryOne("news.queryreadId", map);
			if(num == 0) {
			String userName = (String) baseService.queryOne("user.queryUserName", COMMITERCODE);
			map.put("COMMITER", userName);
			//��ӹ����Ѷ|�¼
			newsViewService.insertReadStatus(map);
			}
			detail = newsViewService.detail(parms);
		}
		return SUCCESS;
	}
	
	/**
	 * 通知公告审批
	 * @return
	 * 2012-2-19 下午02:03:24
	 */
	public String bxSubmit() {
		//获取ID
		String id = this.getRequest().getParameter("id");
		//用户代号
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(id.isEmpty() || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		//任务唯一标识taskId
		String taskId =  this.getRequest().getParameter("taskId");
		//处理意见
		String commentContent =  this.getRequest().getParameter("comment");
		try {
			String s = new String(commentContent.getBytes("iso-8859-1"),"utf-8");
			commentContent = java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}

		//流向
		String name = this.getRequest().getParameter("name");
		//处理类型
		String typeaccept = this.getRequest().getParameter("pass");
		
		Map param = new HashMap();
		param.put("COMMITERCODE",COMMITERCODE );
		param.put("taskId", taskId);
		param.put("comment",commentContent );
		param.put("name", name);
		param.put("pass",typeaccept );
		param.put("ID",id );
		Map mapUuid = (Map) baseService.queryOne("user.queryAll", COMMITERCODE);
		param.put("UuitId",mapUuid.get("ORGID"));
		bxSubmit = newsViewService.bxSubmit(param);
		return SUCCESS;
	}
	
	public String sent() {
		String commitercode = this.getRequest().getParameter("COMMITERCODE");
		String usercode = (String) this.getSession().get(Constants.USERINFO_USERCODE);
		if(null == usercode || "".equals(usercode)) {
		this.getSession().put(Constants.USERINFO_USERCODE, commitercode);
		}
		return SUCCESS;
	}
}
